package com.FH.util;

import org.codehaus.jettison.json.JSONException;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * 执行内部调用restful接口公共类型
 * @author shengyunfeng 2015-10-02
 *
 */
public class InternalInterface {



	//外部接口调用get方法
	public static String getHttp(String url) throws IOException{
		String result = "";
		HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();
		conn.setRequestProperty("Accept", "application/json");
		conn.setConnectTimeout(5000);
		conn.setRequestMethod("GET");
//		conn.setRequestProperty("Accept-Charset", "utf-8");  //设置编码语言
//		conn.setRequestProperty("Content-type", "application/json;charset=UTF-8");
		InputStream inStream =  conn.getInputStream();
		BufferedReader in = new BufferedReader(new InputStreamReader(inStream,"UTF-8"));
		String inputLine;
		while ((inputLine = in.readLine()) != null){
			inputLine = new String(inputLine.getBytes(), "GBK");
			result += inputLine;
		}
		in.close();
		return result;
	}

	//外部接口调用post方法
	public static String postHttp(String url,String datajson) throws IOException {

		String result = "";

		HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();
		conn.setDoOutput(true);
		conn.setRequestProperty("Content-Type", "application/json");
		conn.setConnectTimeout(5000);
		conn.setRequestMethod("POST");

		OutputStream outputStream = conn.getOutputStream();
		outputStream.write(datajson.getBytes());
		outputStream.flush();
		BufferedReader responseBuffer = new BufferedReader(new InputStreamReader(
				(conn.getInputStream())));
		String output;
		while ((output = responseBuffer.readLine()) != null) {
			System.out.println(output);
			result += output;
		}

		conn.disconnect();
		return result;
	}


	//token调用post方法
	public static String postTokenHttp(String url,String datajson,String datetime) throws IOException {

		String result = "";

//		String datetime = DateUtils.getDefaultDateTimeToStringe();

		HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();
		conn.setDoOutput(true);
		conn.setRequestProperty("Content-Type", "application/json");
		conn.setRequestProperty("dateTime", datetime);
		conn.setConnectTimeout(5000);
		conn.setRequestMethod("POST");

		OutputStream outputStream = conn.getOutputStream();
		outputStream.write(datajson.getBytes());
		outputStream.flush();
		BufferedReader responseBuffer = new BufferedReader(new InputStreamReader(
				(conn.getInputStream())));
		String output;
		while ((output = responseBuffer.readLine()) != null) {
			System.out.println(output);
			result += output;
		}

		conn.disconnect();
		return result;
	}


	/**
	 * 获取CRM token
	 * @return
	 * @throws JSONException
	 * @throws IOException
	 */
//	public static String getToken(String datetime) throws JSONException, IOException{
//		String securityUrl = "http://10.170.193.2:8080/aos/Interface/Security/SecurityService/validate.restful";
//        String user = "AppUser";
//        String password = "111111";
//        String key = "锦颐养老_CRM_2015_接口_安全秘钥";
////        String datetime = DateUtils.getDefaultDateTimeToStringe();
//        JSONObject messageJson = new JSONObject();
//        messageJson.put("user", user);
//        messageJson.put("password", SecurityUtils.encrypt3DES(key + datetime, password));
//        HashMap<String, String> header = new HashMap<>();
//        header.put("dateTime", datetime);
//
//        String result = postTokenHttp(securityUrl,messageJson.toString(),datetime);
//
//        JSONObject resultJson = new JSONObject(result);
//
//        String token = "";
//        if (resultJson.getString("statusCode").equals("000000")){
//        	token = resultJson.getJSONObject("message").getString("token");
//        }
//        else{
//        	System.out.println(resultJson.getString("statusMessage"));
//        	token = null;
//        }
//        return token;
//	}


	//CRM接口调用post方法
//	public static String postCRMHttp(String url,String datajson,String key) throws IOException, JSONException {
//
//		String result = "";
//		String datetime = DateUtils.getDefaultDateTimeToStringe();
//
//		String token = getToken(datetime);
////		String token = getToken();
////		String key = "锦颐养老_CRM_2015_接口_安全秘钥";
//
////		FileProcess.newlog( "token获取时间：" + datetime);
//
//		HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();
//		conn.setDoOutput(true);
//		conn.setRequestProperty("Content-Type", "application/json");
//		conn.setRequestProperty("dateTime", datetime);
//		conn.setRequestProperty("token", token);
//		conn.setRequestProperty("certificate", SecurityUtils.createCertificate(key, datetime, token));
//		conn.setConnectTimeout(5000);
//		conn.setRequestMethod("POST");
//
//
//
//		OutputStream outputStream = conn.getOutputStream();
//		outputStream.write(datajson.getBytes());
//		outputStream.flush();
//		BufferedReader responseBuffer = new BufferedReader(new InputStreamReader(
//                (conn.getInputStream())));
//		String output;
//        while ((output = responseBuffer.readLine()) != null) {
//            System.out.println(output);
//            result += output;
//        }
//
//		conn.disconnect();
//		return result;
//	}

	//CRM接口调用put方法
//	public static String putCRMHttp(String url,String datajson,String key) throws IOException, JSONException {
//
//		String result = "";
//		String datetime = DateUtils.getDefaultDateTimeToStringe();
//
//		String token = getToken(datetime);
//
////		String token = getToken();
////		String key = "锦颐养老_CRM_2015_接口_安全秘钥";
//
//		HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();
//		conn.setDoOutput(true);
//		conn.setRequestProperty("Content-Type", "application/json");
//		conn.setRequestProperty("dateTime", datetime);
//		conn.setRequestProperty("token", token);
//		conn.setRequestProperty("certificate", SecurityUtils.createCertificate(key, datetime, token));
//		conn.setConnectTimeout(5000);
//		conn.setRequestMethod("PUT");
//
//
//
//		OutputStream outputStream = conn.getOutputStream();
//		outputStream.write(datajson.getBytes());
//		outputStream.flush();
//		BufferedReader responseBuffer = new BufferedReader(new InputStreamReader(
//                (conn.getInputStream())));
//		String output;
//        while ((output = responseBuffer.readLine()) != null) {
//            System.out.println(output);
//            result += output;
//        }
//
//		conn.disconnect();
//		return result;
//	}









	public static void main(String[] args) {
		System.out.println("111111");
	}
}
